package nl.haemers.data.utils 
{
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import nl.haemers.data.constants.DataTypes;
	
	/**
	 * ...
	 * @author Jonathan Haemers
	 * TODO:
		 * Review this class maybe it needs some refactoring!
	 */
	public class ObjectUtils 
	{
		/** Static methods **/
		/** Public methods **/
		/**
		 * Method checks if value is equal to one of the datatypes. 
		 * @param	value - String
		 * @return  Boolean
		 * @see 	nl.haemers.data.utils.DataTypes
		 */
		public static function isDataType(value:String) : Boolean
		{			
			return (value == DataTypes.BOOLEAN || value == DataTypes.INT || value == DataTypes.NUMBER || 
					value == DataTypes.STRING || value == DataTypes.UINT);
		}
		
		public static function isNumerical(value:*) : Boolean
		{
			return !isNaN(Number(value)) && trim(value).length > 0;
		}
		
		/**
		 * Method checks if value is equal to a Collection type.
		 * Note we exclude dictionaries from this method but I'm not sure if that's right... 
		 * @param	value
		 * @return
		 * @see nl.haemers.data.utils.DataTypes
		 */
		public static function isCollectionType(value:String) : Boolean
		{
			return value == DataTypes.ARRAY || isVector(value) || value == DataTypes.VECTOR;// || value == DataTypes.DICTIONARY;
		}
		
		/**
		 * Method checks if value is a Vector
		 * @param	value
		 * @return
		 */
		public static function isVector(value:String) : Boolean
		{
			return value.indexOf("__AS3__.vec::Vector.") === 0 || value == DataTypes.VECTOR;
		}
		
		public static function isDictionary(value:String) : Boolean
		{
			return value.indexOf("flash.utils::Dictionary") === 0 || value == DataTypes.DICTIONARY;
		}
		
		public static function getCollectionItemClass(value:String) : Class
		{
			//__AS3__.vec.Vector.<haemers.data.vo.PageVO>
			//Array
			var strClass:String ;
			if (isVector(value))
			{
				strClass = value.replace("__AS3__.vec::Vector.<", "").replace(">", "");
			}
			else
			{
				strClass = DataTypes.OBJECT;
			}
			
			return getDefinitionByName(strClass) as Class;
		}
		
		public static function getDataType(value:*) : String
		{
			if (value != null)
			{
				if (isNumerical(value))
				{
					return DataTypes.NUMBER;
				}
				if (checkIsBoolean(value))
				{
					return DataTypes.BOOLEAN;
				}
				if (checkIsString(value))
				{
					return DataTypes.STRING;
				}
			}
			return null;
		}
		
		/** Private methods **/
		private static function checkIsBoolean(value:*) : Boolean
		{
			return value === "true" || value === "false";
		}
		
		private static function checkIsString(value:*) : Boolean
		{
			return String(value).length > 0;
		}
		
		private static function trim(value:String) : String
		{
			return value.replace(" ", "");
		}
	}

}